
var datas=[];
var srcresource="";
var resourceName="";
getData();
resourcesSelect();
 /*datas =  [
    {
        "authorityName": "系统管理",
        "authorityId": 1,
        "LAY_INDETERMINATE": false,
        "children":[{
            "authorityName": "用户管理",
            "authorityId": 2,
            "LAY_INDETERMINATE": false
        },
            {
                "authorityName": "查询用户",
                "authorityId": 3,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "添加用户",
                "authorityId": 4,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "修改用户",
                "authorityId": 5,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "删除用户",
                "authorityId": 6,
                "LAY_INDETERMINATE": false
            }]
    },
    {
        "authorityName": "角色管理",
        "authorityId": 7,
        "LAY_INDETERMINATE": false,
        "children":[{
            "authorityName": "查询角色",
            "authorityId": 8,
            "LAY_INDETERMINATE": false
        },
            {
                "authorityName": "添加角色",
                "authorityId": 9,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "修改角色",
                "authorityId": 10,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "删除角色",
                "authorityId": 11,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "角色权限管理",
                "authorityId": 12,
                "LAY_INDETERMINATE": false
            }]

    },
    {
        "authorityName": "权限管理",
        "authorityId": 13,
        "LAY_INDETERMINATE": false,
        "children":[{
            "authorityName": "查询权限",
            "authorityId": 14,
            "LAY_INDETERMINATE": false
        },
            {
                "authorityName": "添加权限",
                "authorityId": 15,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "修改权限",
                "authorityId": 16,
                "LAY_INDETERMINATE": false
            },
            {
                "authorityName": "删除权限",
                "authorityId": 17,
                "LAY_INDETERMINATE": false
            }]
    }];
*/
layui.use(['treetable', 'layer','form','element'], function () {
    var treetables = layui.treetable,layer = layui.layer,$ = layui.jquery,form=layui.form;
    var element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
    var re;


    // 渲染表格
    window.renderTable = function () {
        layer.load(5);
        re = treetables.render({
            elem: '#menusettingtable',
            data: datas,
            treeColIndex: 2,          // 树形图标显示在第几列
            treeSpid: 0,             // 最上级的父级id
            treeIdName: 'code',       // 	id字段的名称
            treePidName: 'pid',    // 	pid字段的名称
            treeDefaultClose: true,     //是否默认折叠
            page: false,
            //treeLinkage: true,      //父级展开时是否自动展开所有子级
            cols: [[

                {title: "菜单名称", field: "authorityName", align: "left"},
                {title: "资源",  field: "url",width:200,align: "center"},//#associated
                {title: "操作", templet: "#updateAndDelete",width:500}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        })
    };

    renderTable(); //刷新表格操作

    $("#newmenu").on("click",function () {
        parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp","新增菜单");
    })
    $("#delmenu").on("click",function () {
        layer.msg("删除");
    })
    form.on('submit(banktAddsubmit)', function(data) {
       //$("#menuid").val(srcresource);
        data.field.id=srcresource
        $.ajax({
            type : 'POST',
            //  url : 'resource/getTree',
            url:'menu/update',
            data:JSON.stringify(data.field),
            async:false,
            dataType:"json",
            contentType: 'application/json',
            success : function (da) {
                if(da.status==200 && da.data==0){
                    layer.msg("关联提交成功", {icon: 6});
                    setTimeout(function(){
                        layer.closeAll();//关闭所有的弹出层
                        getData();
                        form.render();
                        element.render();
                    }, 1000);
                }
            },
            error: function(data) {
                layer.msg("关联提交失败", {icon: 6});
                setTimeout(function(){
                    layer.closeAll();//关闭所有的弹出层
                }, 1000);

            }
        });


       /*
        var url=data.field.code;//关联的资源code
        if(update(url,srcresource)){
            layer.msg("关联提交成功", {icon: 6});
            setTimeout(function(){
                layer.closeAll();//关闭所有的弹出层
            }, 1000);
        }else{
            layer.msg("关联提交失败", {icon: 6});
            setTimeout(function(){
                layer.closeAll();//关闭所有的弹出层
            }, 1000);
        }*/
    })
    form.render();
    element.render();
    /*$("#resourcesradio").append(html);
    element.render();*/
   /* element.init();
    element.render();
    element.render("nav","associated-form");*/
   // element.render("form","associated-form");

});

/**
 * 菜单关联资源
 * @param url
 * @param code
 */
function update(u,d) {
    var flag=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        url:'menu/update',
        data:{"id":d,"url":u},
        async:false,
        dataType:"json",
        contentType: 'application/json',
        success : function (da) {
            if(da.status==200 && da.data==0){
                // datas=da.data
                flag=true;
            }
        },
        error: function(data) {
            //layer.msg("删除失败!");

        }
    });
    return flag;
}
function resourcesSelect() {
    /*<input type="radio" name="sex" value="男" title="男" checked="">
            <input type="radio" name="sex" value="女" title="女">
            <input type="radio" name="sex" value="禁" title="禁用" disabled="">*/
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        url:'resource/selectAll',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 && da.data.length >0){
                var data=da.data;
                var html=initresources(data);
                $("#resourcesradio").append(html);
            }
        },
        error: function(data) {
            layer.msg("资源加载失败!");
        }
    });
}
function  initresources(data) {
    var html="";
    for(var i=0;i<data.length;i++){
        var da=data[i];
        var checker="";
        if(da.url==resourceName && resourceName !=""){
            checker=checked;
        }
        html+='<input type="radio" name="url" value="'+da.code+'" title="'+da.name+'" '+checker+' ><br>';
    }
    return html;
}

/**
 * 菜单关联资源弹窗
 * @param id
 * @param name
 */
function associatedbutton(code,name,id,url) {
    // resourcesSelect();
    srcresource=id;
    resourceName=url;
    layer.open({
        type: 1,
        title: "选择关联",
        area: ['542px', '460px'],
        content:$("#associateddiv")      // content: layui.jquery("#associateddiv")//引用的弹出层的页面层的方式加载修改界面表单
    });
   /* layui.element.render();*/
}
/**
 * 新增/修改
 * @param authorityId  当前选择操作的数据code
 * @param flag  新增类型（T：同级菜单；X：下级菜单）
 * @param type  操作类型（N：新增；M：修改）
 */
function toOpenUpdateLayer(authorityId,flag,type,id) {
    if(type=="M"){
        parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type+"&&id="+id,"修改菜单");
    }
    if(flag=="T"){
        parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type,"新增同级菜单");
    }
    if(flag=="X"){
            parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type,"新增下级菜单");
    }
    if(flag=="C"){
        parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type,"新增菜单");
    }
}

//执行删除
function doDelete(authorityId,authorityName) {
    //确认；如果点击确认删除；否则不删除
   /* layui.use(['layer'], function () {*/
        var layer = layui.layer;
        layer.confirm('确定要删除'+authorityName+'吗？', {icon: 3, title: '确认删除'}, function (index) {
           // layer.alert("删除成功");
           var flag= deleteMenu(authorityId);
           if(flag){
               layer.alert("删除成功！");
               datas= initData(da.data[0].list);
               //刷新表格树
               getData();
               renderTable();
           }else{
               layer.alert("删除失败！");
           }
            //刷新表格树
           // renderTable();
        });
    /*});*/
}
function deleteMenu(code){
    var flag=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        url:'menu/delete',
        data:{"code":code},
        async:false,
        dataType:"json",

        success : function (da) {
            if(da.status==200 && da.data==1){
                // datas=da.data
                flag=true;
            }
        },
        error: function(data) {
            //layer.msg("删除失败!");
        }
    });
    return flag;
}
//获取菜单树
function  getData() {
    $.ajax({
        type : 'POST',
      //  url : 'resource/getTree',
        url:'menu/getTree',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 && da.data.length >0){
               // datas=da.data
                var data=da.data[0];

                var object=[];
                var obj={};
                obj.id=data.id;
                obj.code=data.code;
                obj.name=data.name;
                obj.level=data.level;
                obj.pid=data.pid;
                obj.path=data.path;
                if(obj.level==1){
                    obj.url=data.name;
                }else{
                    obj.url="";
                }

                if(data.list != null){
                    obj.list=initData(data.list);
                }
                obj.authorityName=obj.name;
                obj.authorityId=obj.code;
                obj.LAY_INDETERMINATE=false;
                obj.children=obj.list;
                object.push(obj);
               datas= object;
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
function  initData(data) {
   var object=[];
   for(var i=0;i<data.length;i++){
        var obj={};
            obj.id=data[i].id;
            obj.code=data[i].code;
            obj.name=data[i].name;
            obj.level=data[i].level;
            obj.pid=data[i].pid;
            obj.path=data[i].path;
            if(obj.level==1){
                obj.url=data[i].name;
            }else{
                obj.url="";
            }

            if(data[i].list != null){
                obj.list=initData(data[i].list,data[i].code);
            }
       obj.authorityName=obj.name;
       obj.authorityId=obj.code;
       obj.LAY_INDETERMINATE=false;
       obj.children=obj.list;
       object.push(obj);
   }
   return object;
}
/*

{
    "authorityName": "系统管理",
    "authorityId": 1,
    "LAY_INDETERMINATE": false,
    "children":[{
    "authorityName": "用户管理",
    "authorityId": 2,
    "LAY_INDETERMINATE": false
},
    {
        "authorityName": "删除用户",
        "authorityId": 6,
        "LAY_INDETERMINATE": false
    }]
},*/
